package com.dwxt.cdms.service;

import com.dwxt.cdms.admin.service.HistoryServiceAdmin;
import com.dwxt.cdms.api.common.utils.DateUtil;
import com.dwxt.cdms.base.OperateCategory;
import com.dwxt.cdms.base.Page;
import com.dwxt.cdms.dao.BloodSugarMapper;
import com.dwxt.cdms.dao.poct.Poct4Mapper;
import com.dwxt.cdms.dao.poct.Poct8Mapper;
import com.dwxt.cdms.entity.BloodSugar;
import com.dwxt.cdms.entity.Customer;
import com.dwxt.cdms.entity.poct.Poct4;
import com.dwxt.cdms.entity.poct.Poct8;
import com.dwxt.cdms.entity.vo.BloodSugarCustom;
import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.StatusCode;
import com.dwxt.common.util.IDUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class BloodSugarService {

    @Autowired
    private BloodSugarMapper bloodSugarMapper;

    @Autowired
    private HistoryServiceAdmin historyServiceAdmin;

    @Autowired
    private StationService stationService;

    @Autowired
    private Poct4Mapper poct4Mapper;

    @Autowired
    private Poct8Mapper poct8Mapper;

    public BaseResult selectSugarPage(Page page, Customer customer, Integer isAbnormal, String start, String end,
                                      String customerId,String partId) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<BloodSugarCustom> maps = bloodSugarMapper.selectSugarPage(customer, isAbnormal, start, end, customerId,partId);
        maps.forEach(bloodSugarCustom -> {
            String bloodSugar = bloodSugarCustom.getBloodSugar();
            bloodSugarCustom.setBloodSugar(strToDouble(bloodSugar));
        });
        PageInfo<BloodSugarCustom> pageInfo = new PageInfo<>(maps);
        return BaseResult.ok(pageInfo);
    }

    /**
     * @方法名：strToDouble
     * @描述： 转换血糖格式为【mmol/L】
     * @作者： kjz
     * @日期： Created in 2020/9/28 17:58
     */
    public String strToDouble(String bloodSugar) {
        double doubleBloodSugar = Double.parseDouble(bloodSugar);
        doubleBloodSugar = doubleBloodSugar / 18;
        //保留一位小数点，四舍五入
        String result = String.format("%.1f", doubleBloodSugar);
        return result;
    }

    public BaseResult insert(BloodSugar bloodSugar) {
        bloodSugar.setId(IDUtils.genItemId());
        bloodSugarMapper.insertSelective(bloodSugar);
        //插入操作记录
        historyServiceAdmin.generate("新增血糖记录:" + bloodSugar.getId(), OperateCategory.INSERT);
        return BaseResult.ok();
    }

    public BaseResult update(BloodSugar bloodSugar) {
        bloodSugarMapper.updateByPrimaryKeySelective(bloodSugar);
        return BaseResult.ok();
    }

    public BaseResult delete(String id) {
        bloodSugarMapper.deleteByPrimaryKey(id);
        return BaseResult.ok();
    }

    public BaseResult selectSugarPageGroup(Page page, Customer customer, Integer isAbnormal, String start, String end
            , Integer mealtime) {
        String pa = mealtime == null ? null : mealtime + "";
        Integer stationId = stationService.getLoginUserStation();
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<BloodSugarCustom> maps = bloodSugarMapper.selectSugarPageGroup(customer, isAbnormal, start, end, pa,stationId);
        maps.forEach(bloodSugarCustom -> {
            String bloodSugar = bloodSugarCustom.getBloodSugar();
            bloodSugarCustom.setBloodSugar(strToDouble(bloodSugar));
        });
        PageInfo<BloodSugarCustom> pageInfo = new PageInfo<>(maps);
        return BaseResult.ok(pageInfo);
    }

    public List<BloodSugar> selectCustomerSugar(String customerId, Integer scope,Integer partId) {
        return bloodSugarMapper.selectCustomerSugar(customerId, scope,partId);
    }

    public BaseResult updatePart(Map map) {
        List ids = (List) map.get("ids");
        Integer partId = (Integer) map.get("partId");
        if (partId == null) {
            return BaseResult.build(StatusCode.NO_PART_ID);
        }
        int i = bloodSugarMapper.updatePart(ids, partId);
        return BaseResult.ok(i);
    }

    public BaseResult selectCustomerSugarFromPoctOrAi(Integer customerId, Integer scope, Integer partId) {
        List<BloodSugar> bloodSugars = bloodSugarMapper.selectCustomerSugar(customerId.toString(), scope, partId);
        if (bloodSugars != null && bloodSugars.size() > 0) {
            return BaseResult.ok(bloodSugars);
        }
        List<Poct4> poct4List = poct4Mapper.dataList(customerId, "10",partId,scope);
        if (poct4List != null && poct4List.size() > 0) {
            List<BloodSugar> bloodSugars4 = poct4List.stream().map(poct4 -> {
                BloodSugar bloodSugar = new BloodSugar();
                bloodSugar.setCustomerId(poct4.getCustomerId());
                bloodSugar.setBloodSugar(poct4.getValue());
                if (scope == 1) {
                    bloodSugar.setCheckTime(DateUtil.parseStr(poct4.getCheckTime(), DateUtil.FMT_YYYYMMDD));
                } else {
                    bloodSugar.setCheckTime(DateUtil.parseStr(poct4.getCheckTime(), DateUtil.FMT_YYYYMMDD_HHMMSS));
                }
                return bloodSugar;
            }).collect(Collectors.toList());
            return BaseResult.ok(bloodSugars4);
        }
        List<Poct8> poct8List = poct8Mapper.dataList(customerId, "10",partId,scope);
        List<BloodSugar> bloodSugars8 = poct8List.stream().map(poct8 -> {
            BloodSugar bloodSugar = new BloodSugar();
            bloodSugar.setCustomerId(poct8.getCustomerId());
            bloodSugar.setBloodSugar(poct8.getValue());
            if (scope == 1) {
                bloodSugar.setCheckTime(DateUtil.parseStr(poct8.getCheckTime(), DateUtil.FMT_YYYYMMDD));
            } else {
                bloodSugar.setCheckTime(DateUtil.parseStr(poct8.getCheckTime(), DateUtil.FMT_YYYYMMDD_HHMMSS));
            }
            return bloodSugar;
        }).collect(Collectors.toList());

        return BaseResult.ok(bloodSugars8);
    }
}
